//
//  BBGradientBackView.m
//  BBCoverFlowTest
//
//  Created by ben smith on 4/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBGradientBackView.h"
#import "BBFingerMarkerView.h"

@implementation BBGradientBackView

@synthesize backgroundGradient;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
			[self setupBackgroundGradient];
			//fingerMarkerView = [[BBFingerMarkerView alloc] initWithFrame:frame];
			//[self addSubview:fingerMarkerView];
			[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"BBGradientStartColor" options:NSKeyValueObservingOptionNew context:nil];
			[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"BBGradientEndColor" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)_addSasflkhjsadlkfubview:(NSView*)aView
{
	[super addSubview:aView];
	if (aView != fingerMarkerView) {
		// then move fingermarker to the top
		[fingerMarkerView removeFromSuperview];
		[self addSubview:fingerMarkerView];
	}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self setupBackgroundGradient];
	[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect {
    // Drawing code here.
	[self.backgroundGradient drawInRect:self.bounds angle:-90.0];
}

- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	NSData * topData = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBGradientStartColor"];
	NSColor * gradientTop;
	if (topData == nil) {
		CGFloat red1   =   0.10;
		CGFloat green1 =   0.20;
		CGFloat blue1  =  0.250;
		gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	} else {
		gradientTop 	= [NSUnarchiver unarchiveObjectWithData:topData];
	}
	NSData * bottomData = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBGradientEndColor"];
	NSColor * gradientBottom;
	if (bottomData == nil) {
		CGFloat red1   =   0.10;
		CGFloat green1 =   0.20;
		CGFloat blue1  =  0.250;
		gradientBottom    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	} else {
		gradientBottom 	= [NSUnarchiver unarchiveObjectWithData:bottomData];
	}
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}

@end
